package cn.tedu.um.dao.impl;

import cn.tedu.um.dao.UserDAO;
import cn.tedu.um.entity.User;
import cn.tedu.um.util.DBUtil;

import javax.xml.transform.Result;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDAOImpl implements UserDAO {
    @Override
    public int insertUser(User user) {
        return 0;
    }

    @Override
    public int updateUser(User user) {

        return 0;
    }

    @Override
    public int deleteUserById(int id) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int row = -1;
        try {
            conn = DBUtil.getConnection();
            String sql = "delete from  user where id=?";

            ps = conn.prepareStatement(sql);
            System.out.println(sql);
            ps.setInt(1,id);
            row = ps.executeUpdate();

        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        return row;
    }

    @Override
    public User selectUserById(int id) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        User user  = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "select * from user where id=?";

            ps = conn.prepareStatement(sql);
            System.out.println(sql);
            ps.setInt(1,id);
            rs = ps.executeQuery();
            if(rs.next()){
                user = new User(
                        rs.getInt("id"),
                        rs.getString("name"),
                        rs.getString("username"),
                        rs.getString("password"));
            }
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        return user;
    }

    @Override
    public User selectUserByUsernameAndPassword(String username, String password) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        User user  = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "select * from user where username=? and password=?";

            ps = conn.prepareStatement(sql);
            System.out.println(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            rs = ps.executeQuery();
            while(rs.next()){
                user = new User(
                        rs.getInt("id"),
                        rs.getString("name"),
                        rs.getString("username"),
                        rs.getString("password"));
            }
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        return user;
    }

    @Override
    public List<User> selectUserByUsername(String username) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<User> userList = new ArrayList<>();
        try {
            conn = DBUtil.getConnection();
            String sql = "select * from user where username like \"%\"?\"%\"";

            ps = conn.prepareStatement(sql);
            System.out.println(sql);
            ps.setString(1,username);
            rs = ps.executeQuery();
            while(rs.next()){
                User user = new User(
                        rs.getInt("id"),
                        rs.getString("name"),
                        rs.getString("username"),
                        rs.getString("password"));
                userList.add(user);
            }
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        return userList;
    }

    @Override
    public List<User> selectAllUsers() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<User> userList = new ArrayList<>();
        try {
            conn = DBUtil.getConnection();
            String sql = "select * from user";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                User user = new User(
                        rs.getInt("id"),
                        rs.getString("name"),
                        rs.getString("username"),
                        rs.getString("password"));
                userList.add(user);
            }
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        return userList;
    }


}
